package pure.ui 
{
	import pure.ui.core.Component;
	import pure.ui.skins.Skin;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.TextEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	
	/**
	 * @author des
	 * 文本输入框
	 */
	
public class InputText extends Component 
{
	public function InputText(defaultHtml:String,x:Number, y:Number,size:Point=null) 
	{
		super(x, y, null);
		this._text.htmlText = defaultHtml;
		if (size) {
			initialize(size.x, size.y);
		}
		else {
			initialize(124, 24);
		}
		setStyle();
		setBackground();	
	}
	

	protected var _text:TextField;
	protected var _style:TextFormat;
	protected var _haveBorder:Boolean;
	
	protected var ____enterObserver:Function;
	protected var _backgroundColor:uint;
	protected var _thickness:Number;
	protected var _alpha:Number;
		
		
	/**
	 * 初始化
	 * <TODO>使用方法替代setSize方法
	 **/
	public function initialize(w:Number, h:Number, gapX:Number = 2, gapY:Number = 1 ):void
	{
		super.setSize(w, h);
		this._text.x = gapX;
		this._text.width = w - (gapX << 1);
		this._text.y = gapY;
		this._text.height = h - (gapY << 1);
	}
	
	/**
	 * 文本样式
	 **/
	public function setStyle(color:uint = 0xff9c00 , size:int = 16):void
	{
		this._text.textColor = color;
		this._style.size = size;
		if (this._text.text == ''||this._text.text==null) {
			this._text.defaultTextFormat = this._style;
		}
		else {
			this._text.setTextFormat(this._style);
		}
	}
	
	/**
	 * 背景
	 **/
	public function setBackground(backgroundColor:uint=0xcccccc,alpha:Number=0.7,thickness:Number=2):void
	{
		this._backgroundColor = backgroundColor;
		this._alpha = alpha;
		this._thickness = thickness;
		update();
	}
	
	/**
	 * 文本边框
	 **/
	public function setBorder(haveBorder:Boolean, color:uint = 0, blur:int = 4, strength:int = 22 ):void
	{
		if (this._haveBorder != haveBorder) {
			this._haveBorder = haveBorder;
			if (this._haveBorder) {
				_text.filters = [new GlowFilter(color, 0.9, blur, blur , strength) ];
			}
			else {
				_text.filters = null;
			}
		}
	}
	
	/**
	 * 确认回调
	 **/
	public function setObserverEnter(enterObserver:Function):void
	{
		if (enterObserver != null) {
			if (stage) {
				____onAddedToStage();
			}
			else{
				addEventListener(Event.ADDED_TO_STAGE, ____onAddedToStage);	
			}
			this.____enterObserver = enterObserver;
		}
		else {
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, ____onKeyDown);
		}
	}
	
	
	
	
	/* ----------------------------------------------------    get / set    ----------------------------------------------------*/
	
	
	
	
	public function set text(value:String):void
	{
		this._text.text = value;
	}
	
	public function get text():String
	{
		return this._text.text;
	}
	
	public function set displayAsPassword(b:Boolean):void
	{
		if (this._text.displayAsPassword != b) {
			this._text.displayAsPassword = b;
		}
	}
	
	public function get displayAsPassword():Boolean
	{
		return this._text.displayAsPassword;
	}
	
	public function set restrict(value:String):void
	{
		this._text.restrict = value;
	}
	
	public function get restrict():String
	{
		return this._text.restrict;
	}
	
	public function set maxChars(value:int):void
	{
		this._text.maxChars = value;
	}
	
	
	
	
	/* ----------------------------------------------------    private    ----------------------------------------------------*/
	
	
	
	
	override protected function _initSelf():void
	{
		super._initSelf();
		this._haveBorder = false;
	}
	
	override protected function _addChildren():void
	{
		this._text = new TextField();
		this._text.multiline = true;
		this._text.wordWrap = true;
		this._text.type = TextFieldType.INPUT;
		addChild(this._text);
		this._style = new TextFormat();
	}
	
	override protected function _draw():void
	{
		_drawNoSkin(this._backgroundColor, this._thickness, 1, this._alpha);
	}
	
	protected function ____onAddedToStage(e:Event=null):void
	{
		removeEventListener(Event.ADDED_TO_STAGE, ____onAddedToStage);
		stage.addEventListener(KeyboardEvent.KEY_DOWN, ____onKeyDown);
	}
	
	protected function ____onKeyDown(e:KeyboardEvent):void
	{
		if (stage.focus == this._text) {
			if (e.keyCode == Keyboard.ENTER) {
				if (____enterObserver != null) {
					____enterObserver();
				}
			}
		}
	}
}

}